// pages/ocr/uploadpic.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgFilePaths: [],
    cWidth: 0,
    cHeight: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },
  /**
   * 从相册获取图片
   */
  chooseLocalPic: function(event) {
    let that = this
    wx.chooseImage({
      count: 1, //最多可以选择的图片张数
      sizeType: ['original', 'compressed'], //所选的图片的尺寸
      sourceType: ['album'], //albun 从相册； camera 从摄像机
      success: function(res) {
        //1.界面显示img，设置地址
        that.setData({
          imgFilePaths: res.tempFilePaths
        })
        //2.压缩, 10压缩率，10MB的base64，变成2MB不到；降低的是图片质量
        wx.compressImage({
          src: res.tempFilePaths[0], // 图片路径
          quality: 10, // 压缩质量
          success: function(res) {
            console.log("压缩成功！")
            console.log(res.tempFilePath)
            that.setData({
              imgFilePaths: [res.tempFilePath]
            })
          }
        })
      },
    })

  },
  /**
   * 从相机获取图片
   */
  fromCam: function(event) {
    let that = this
    wx.chooseImage({
      count: 1, //最多可以选择的图片张数
      sizeType: ['compressed'], //所选的图片的尺寸
      sourceType: ['camera'], //albun 从相册； camera 从摄像机
      success: function(photos) {
        wx.getImageInfo({
          src: photos.tempFilePaths[0],
          success: function(res) {
            //---------利用canvas压缩图片--------------
            //固定宽是1024
            var canvasWidth = 1024
            var canvasHeight = Math.floor(canvasWidth * res.height / res.width)
            that.setData({
              cWidth: canvasWidth,
              cHeight: canvasHeight
            })
            //----------绘制图形并取出图片路径--------------
            var ctx = wx.createCanvasContext('canvas')
            ctx.drawImage(res.path, 0, 0, canvasWidth, canvasHeight)
            ctx.draw(false, setTimeout(function() {
              wx.canvasToTempFilePath({
                canvasId: 'canvas',
                destWidth: canvasWidth,
                destHeight: canvasHeight,
                fileType: "jpg", //jpg or png
                quality: 0.9, //jpg 图片质量
                success: function(rescanvas) {
                  console.log(rescanvas.tempFilePath) //最终图片路径
                  that.setData({
                    imgFilePaths: [rescanvas.tempFilePath]
                  })
                },
                fail: function(rescanvas) {
                  console.log(rescanvas.errMsg)
                }
              })
            }, 100)) //留一定的时间绘制canvas
          },
          fail: function(res) {
            console.log(res.errMsg)
          }
        })
      },
    })
  },

  /**
   * 预览
   */
  preViewPic: function(event) {
    if (this.data.imgFilePaths.length > 0) {
      wx.previewImage({
        urls: this.data.imgFilePaths,
      })
    }
  },

  /**
   * 上传图片
   */
  uploadimg: function(event) {
    //4.img转base64
    console.log(this.data.imgFilePaths[0])
    if (this.data.imgFilePaths[0] != undefined) {
      wx.request({
        url: 'http://service-ddu5fftz-1257156827.sh.apigw.tencentcs.com/put_base642img2COS',
        method: 'POST',
        data: {
          "image": wx.getFileSystemManager().readFileSync(this.data.imgFilePaths[0], "base64")
        }
      })
    }
  }
})